<?php

namespace App\Http\Controllers\CallingCards\Feishu;

use App\Http\Controllers\BaseController;
use App\Models\FeishuCallingCard\FeishuCallingCardPosterBackground;
use App\Service\CallingCards\CardService;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Kuafu\File\FileUtil;

class CardController extends BaseController
{
    /*
     * 获取飞书用户信息
     */
    public function index(Request $request)
    {
        $info                     = CardService::cardInfo(auth('callingCardsFeishu')->user());
        $info['backgroundImages'] = FeishuCallingCardPosterBackground::query()
            ->where('status', 1)
            // 为什么不生效
            ->where('startDate', '<=', Carbon::now()->toDateString())
            ->where('endDate', '>=', Carbon::now()->toDateString())
            ->orderBy('sort', 'desc')
            ->orderBy('id', 'desc')
            ->get()
            ->map(function ($b) {
                return [
                    'id'   => $b->id,
                    'url'  => FileUtil::domain() . '/' . $b->previewImageUrl . '?x-oss-process=image/resize,h_1334/quality,q_80',
                    'path' => $b->previewImageUrl,
                    'type' => $b->type,
                ];
            })->toArray();
        return $this->success($info);
    }

    public function backgrounds()
    {
        $list   = [];
        $images = FeishuCallingCardPosterBackground::query()
            ->where('status', 1)
            // 为什么不生效
            ->where('startDate', '<=', Carbon::now()->toDateString())
            ->where('endDate', '>=', Carbon::now()->toDateString())
            ->orderBy('sort', 'desc')
            ->orderBy('id', 'desc')
            ->get()
            ->map(function ($b) {
                return [
                    'id'   => $b->id,
                    'url'  => FileUtil::domain() . '/' . $b->previewImageUrl . '?x-oss-process=image/resize,h_1334/quality,q_80',
                    'path' => $b->previewImageUrl,
                    'type' => $b->type,
                ];
            })->toArray();
        foreach ($images as $item) {
            $type = '全部';
            if (!isset($list[$type])) {
                $list[$type] = [
                    'type'  => $type,
                    'items' => [],
                ];
            }
            $list[$type]['items'][] = [
                'id'   => $item['id'],
                'url'  => $item['url'],
                'path' => $item['path'],
            ];
        }
        foreach ($images as $item) {
            $type = strlen($item['type']) ? $item['type'] : '全部';
            if ('全部' == $type) {
                continue;
            }
            if (!isset($list[$type])) {
                $list[$type] = [
                    'type'  => $type,
                    'items' => [],
                ];
            }
            $list[$type]['items'][] = [
                'id'   => $item['id'],
                'url'  => $item['url'],
                'path' => $item['path'],
            ];
        }
        $list = array_values($list);
        return $this->success(compact('list'));
    }

    /*
     * 修改
     */
    public function save(Request $request)
    {
        $request->validate([
            'name'             => 'required',
            'mobile'           => 'required|int',
            'position'         => 'present',
            'showPosition'     => 'required|boolean',
            'companyAddressId' => 'required|int|min:1',
            'wechatId'         => 'present',
            'avatar'           => 'required',
        ]);
        CardService::save(auth('callingCardsFeishu')->user(), $request->all());
        return $this->success();
    }

    /*
     * 获取我的线索
     */
    public function getMyClue(Request $request)
    {
        $pageSize = (int)$request->get('pageSize', 20);
        $pageNum  = (int)$request->get('pageNum', 1);
        return $this->success(CardService::getMyClue(auth('callingCardsFeishu')->user()->id, $pageSize, $pageNum));
    }

    public function updateBackground(Request $request)
    {
        $this->validate($request, [
            'id' => 'required|int',
        ]);
        $posterBackground = FeishuCallingCardPosterBackground::find($request->id);
        if (null === $posterBackground) {
            return $this->fail(404, '背景图不存在');
        }
        auth('callingCardsFeishu')->user()->backgroundId = $request->id;
        auth('callingCardsFeishu')->user()->refreshWechatQrcode();
        auth('callingCardsFeishu')->user()->save();
        return $this->success([
            'wechatQrcode' => FileUtil::domain() . '/' . auth('callingCardsFeishu')->user()->wechatQrcode . '?x-oss-process=image/resize,w_750/quality,q_80',
        ]);
    }
}
